package cn.pconline.autoclub.service.bbssync;

import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import cn.pconline.autoclub.entity.Club;
import cn.pconline.autoclub.entity.ClubIntegral;
import cn.pconline.autoclub.entity.ClubMember;
import cn.pconline.autoclub.entity.DynaInfo;
import cn.pconline.autoclub.entity.User;
import cn.pconline.autoclub.service.DynaIndexService;
import cn.pconline.autoclub.service.DynaService;
import cn.pconline.autoclub.service.PetrolService;
import cn.pconline.officialclub.service.ODynaService;

import com.alibaba.fastjson.JSONObject;

/**
 * @author zhouruxuan
 * @date 2014年12月4日 上午10:21:18
 * {"agent":0,"authorId":72402,"createAt":1418293790000,"createIp":"192.168.32.66","key":"autobbs.topic.delete5805701","routeKey":"autobbs.topic.delete","tid":5805701}
 */
public class SyncDeleteTopicService extends BbsSyncPetrolService {

    @Autowired
    private DynaService dynaService;
    @Autowired
    private PetrolService petrolService;
    @Autowired
    private ODynaService oDynaService;
    @Autowired
    private DynaIndexService dynaIndexService;

    @Override
    protected int addPetroltoMember(ClubMember clubMember, JSONObject msg) {
        int total = 0;
        DynaInfo dynaInfo = dynaService.findDynaInfoByTopicId(getTopicId(msg), getCreateAt(msg));
        if (dynaInfo == null) {
            return total;
        }
        // 如果被删帖子是精华帖，则减精华分数
        if (dynaInfo.getPick() > 0) {
            ClubIntegral i = petrolService.getById(PetrolService.ADD_PERSONAL_PETROL_ITEM_PICK_CANCEL);
            String reason = i.getName() + "(topic-" + dynaInfo.getTopicId() + ")";
            petrolService.addOnlyPersonWithinLimit(clubMember, (int) i.getIntegralId(), i.getPetrol(), reason, false);
            total += i.getPetrol();
        }

        // 如果被删帖子是推荐帖，则减推荐分数
        if (dynaInfo.getRecommend() > 0) {
            ClubIntegral i = petrolService.getById(PetrolService.ADD_PERSONAL_PETROL_ITEM_RECOMMEND_CANCEL);
            String reason = i.getName() + "(topic-" + dynaInfo.getTopicId() + ")";
            petrolService.addOnlyPersonWithinLimit(clubMember, (int) i.getIntegralId(), i.getPetrol(), reason, false);
            total += i.getPetrol();
        }

        ClubIntegral i = petrolService.getById(PetrolService.ADD_PERSONAL_PETROL_ITEM_TOPIC_DELETE);
        String reason = i.getName() + "(topic-" + dynaInfo.getTopicId() + ")";
        petrolService.addOnlyPersonWithinLimit(clubMember, (int) i.getIntegralId(), i.getPetrol(), reason, false);
        total += i.getPetrol();
        return total;
    }

    @Override
    protected int addPetroltoClub(Club club, JSONObject msg) {
        int total = 0;
        DynaInfo dynaInfo = dynaService.findDynaInfoByTopicId(getTopicId(msg), getCreateAt(msg));
        if (dynaInfo == null) {
            return total;
        }
        // 如果被删帖子是精华帖，则减精华分数
        if (dynaInfo.getPick() > 0) {
            ClubIntegral i = petrolService.getById(PetrolService.ADD_PERSONAL_PETROL_ITEM_PICK_CANCEL);
            String reason = i.getName() + "(topic-" + dynaInfo.getTopicId() + ")";
            petrolService.addToClubWithinLimit(club.getClubId(), (int) i.getIntegralId(), i.getPetrol(), reason, false);
            total += i.getPetrol();
        }

        // 如果被删帖子是推荐帖，则减推荐分数
        if (dynaInfo.getRecommend() > 0) {
            ClubIntegral i = petrolService.getById(PetrolService.ADD_PERSONAL_PETROL_ITEM_RECOMMEND_CANCEL);
            String reason = i.getName() + "(topic-" + dynaInfo.getTopicId() + ")";
            petrolService.addToClubWithinLimit(club.getClubId(), (int) i.getIntegralId(), i.getPetrol(), reason, false);
            total += i.getPetrol();
        }

        ClubIntegral i = petrolService.getById(PetrolService.ADD_PERSONAL_PETROL_ITEM_TOPIC_DELETE);
        String reason = i.getName() + "(topic-" + dynaInfo.getTopicId() + ")";
        petrolService.addToClubWithinLimit(club.getClubId(), (int) i.getIntegralId(), i.getPetrol(), reason, false);
        total += i.getPetrol();
        return total;
    }

    @Override
    protected long getUserId(JSONObject msg) {
        return msg.getLongValue("authorId");
    }

    @Override
    public void process(JSONObject msg, ClubMember clubMember) {
        //验证用户
        long userId = getUserId(msg);
        if (userId <= 0L) {
            log.info("not userId-{}", userId);
            return;
        }

        //判断用户是否登录
        User user = userService.authUser(userId);
        if (user == null || user.getUid() == 0L) {
            log.info("not found user-{}", userId);
            return;
        }
        DynaInfo dynaInfo = dynaService.findDynaInfoByTopicId(getTopicId(msg), getCreateAt(msg));
        if (dynaInfo == null) {
            log.info("not found dynaInfo");
            return;
        }
        //删除帖子
        oDynaService.deleteClubDyna(dynaInfo.getDynaInfoId());//删除官方车友会动态索引
        dynaService.deleteClubDyna(dynaInfo.getClubId(), dynaInfo.getDynaInfoId());//修改车友会动态分表
        dynaService.deleteUserDyna(dynaInfo.getCreateBy(), dynaInfo.getDynaInfoId());//修改用户动态分表
        dynaService.deleteCityDyna(dynaInfo.getDynaInfoId(), dynaInfo.getProvinceId(), dynaInfo.getCityId());
        //删除动态索引
        dynaIndexService.deleteDynaIndex(dynaInfo.getDynaInfoId());
    }

    private long getTopicId(JSONObject msg) {
        return msg.getLongValue("topicId");
    }

    private Date getCreateAt(JSONObject msg) {
        return msg.getDate("createAt");
    }
}
